#pragma once

#include "esp_matter.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <map>
#include <string>
#include <app/clusters/scenes-server/SceneTable.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <app/CommandHandler.h>
#include <app/ConcreteCommandPath.h>
#include <app/cluster-building-blocks/QuieterReporting.h>
#include <app/data-model/Nullable.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
#include <app/util/basic-types.h>
#include <app/util/config.h>
#include <platform/CHIPDeviceConfig.h>
#include <protocols/interaction_model/StatusCode.h>

class ThermostatServer
{
public:
    static constexpr int16_t kDefaultAbsMinHeatSetpointLimit = 700;  // 7C (44.5 F) is the default
    static constexpr int16_t kDefaultAbsMaxHeatSetpointLimit = 3000; // 30C (86 F) is the default
    static constexpr int16_t kDefaultMinHeatSetpointLimit = 700;     // 7C (44.5 F) is the default
    static constexpr int16_t kDefaultMaxHeatSetpointLimit = 3000;    // 30C (86 F) is the default
    static constexpr int16_t kDefaultAbsMinCoolSetpointLimit = 1600; // 16C (61 F) is the default
    static constexpr int16_t kDefaultAbsMaxCoolSetpointLimit = 3200; // 32C (90 F) is the default
    static constexpr int16_t kDefaultMinCoolSetpointLimit = 1600;    // 16C (61 F) is the default
    static constexpr int16_t kDefaultMaxCoolSetpointLimit = 3200;    // 32C (90 F) is the default
    static constexpr int16_t kDefaultHeatingSetpoint = 2000;
    static constexpr int16_t kDefaultCoolingSetpoint = 2600;
    static constexpr int8_t kDefaultDeadBand = 25; // 2.5C is the default

private:
    uint16_t endpoint_id;
    esp_matter::cluster_t *thermostat_cluster;
    TaskHandle_t temperature_task_handle;
    std::map<uint8_t, std::string> m_stored_scenes; // To store scene ID and name

    // 温度模拟任务（静态成员函数，用于FreeRTOS任务）
    static void temperature_simulation_task(void *pvParameters);

    ThermostatServer() : endpoint_id(0), thermostat_cluster(nullptr), temperature_task_handle(nullptr) {};

    static ThermostatServer instance;

public:
    using Feature = chip::app::Clusters::Thermostat::Feature;

    static ThermostatServer &Instance() { return instance; }

    chip::scenes::SceneHandler *GetSceneHandler();

    bool HasFeature(chip::EndpointId endpoint, Feature feature);

    // 创建温控器设备
    esp_matter::endpoint_t *create_device(esp_matter::node_t *node);

    // 获取端点ID
    uint16_t get_endpoint_id() const { return endpoint_id; }

    // 停止温度模拟
    void stop_temperature_simulation();

    // 设置温度值
    void set_temperature(int16_t temperature);

    // 获取当前温度（如果需要的话）
    int16_t get_current_temperature() const;

    // 场景ID定义
    enum SceneId
    {
        SCENE_HOME = 0,   // 回家模式
        SCENE_AWAY = 1,   // 离家模式
        SCENE_SLEEP = 2,  // 睡眠模式
        SCENE_WINTER = 3, // 冬季模式
        SCENE_SUMMER = 4  // 夏季模式
    };


private:
    // 内部方法，用于更新温控器属性
    void set_system_mode(uint8_t mode);
    void set_heating_setpoint(int16_t setpoint);
    void set_cooling_setpoint(int16_t setpoint);


    uint16_t getEndpointIndex(chip::EndpointId endpoint);

    friend class ThermostatSceneHandler;
};


